from fastapi import APIRouter
from fastapi.staticfiles import StaticFiles
from fastapi.requests import Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

from services.ad936x.api import Ad936xApi
from services.ad936x.routers import ad936x_router
from services.attenuators.routers import attenuators_router
from services.control.routers import control_router
from services.killer.routers import killer_router
from services.relays_module.routers import relays_module_router
from services.dsp.routers import dsp_router
from services.ocb.routers import ocb_router
from services.new_services.new_server_integration.routers import new_server_integration_router
from services.server_integration.routers import server_integration_router
from services.services_module.routers import services_module_router
from services.automatic_control.routers import ac_router
from services.telemetry.routers import telemetry_router
from services.new_services.technic_page.routers import technic_router

from settings import settings

services_router = APIRouter(prefix='')

services_router.include_router(router=technic_router)
services_router.include_router(router=new_server_integration_router)
services_router.include_router(router=server_integration_router)


if not settings.OFF_ROUTERS:
    services_router.include_router(router=ad936x_router)
    services_router.include_router(router=control_router)
    services_router.include_router(router=relays_module_router)
    services_router.include_router(router=dsp_router)
    services_router.include_router(router=ocb_router)
    services_router.include_router(router=attenuators_router)
    services_router.include_router(router=services_module_router)
    services_router.include_router(router=ac_router)
    services_router.include_router(router=telemetry_router)
    services_router.include_router(router=killer_router)

    ad_api = Ad936xApi()
    @services_router.get('/cp', response_class=HTMLResponse)
    async def control_panel(request: Request):
        configs = await ad_api.get_all_configs()
        spectrums_number = settings.SPECTRUMS_NUMBER
        attenuators_number = settings.ATTENUATORS_NUMBER
        services_number = settings.SERVICES_MODULE_NUMBER
        relays_number = settings.SERVICES_RELAYS_NUMBER
        dsp_number = settings.DSP_NUMBER
        services_relays_number = settings.SERVICES_RELAYS_NUMBER
        ocb_method_keys = settings.OCB_METHODS_KEYS
        service_names = settings.SERVICES_NAME_STATIC
        return (Jinja2Templates(directory=f'{settings.APP_PATH}/templates')
                .TemplateResponse('control_panel.html', {'request': request,
                     'configs': configs,
                     'spectrums_number': spectrums_number,
                     'attenuators_number': attenuators_number,
                     'services_number': services_number,
                     'relays_number': relays_number,
                     'dsp_number': dsp_number,
                     'services_relays_number': services_relays_number,
                     'ocb_method_keys': ocb_method_keys,
                     'service_names': service_names,
                     }))

